import { createRouter, createWebHashHistory } from 'vue-router';
import { useUserStore } from '@/store/user';

import routes from './routes/index';

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach(async (to, form, next) => {
  const user = useUserStore();
  if (to.path === '/login' && !user.userInfo?.token) {
    next();
  } else {
    if (!user.userInfo?.token) {
      next(`/login`);
    } else if (user.userInfo?.token && to.path === '/login') {
      next('/home');
    } else {
      next();
    }
  }
});

export default router;
